package dfbz.com.dao;

import dfbz.com.dao.base.BaseDao;
import dfbz.com.model.UserInfo;
import dfbz.com.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.SQLException;
import java.util.Map;

public class UserInfoDao extends BaseDao<UserInfo> {

    /**
     * 获取用户详细信息的sql
     *
     * @param userId 用户ID
     * @return Map对象
     */
    public Map<String, Object> getDetail(Integer userId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        Map<String, Object> map = null;
        try {
            String sql = "select ui.user_id as userId,ui.real_name as realName,ui.age,ui.gender,u.is_secret as isSecret," +
                    "ui.phone,ui.describe as introduce,ui.register_time as registerTime,ui.login_time as loginTime," +
                    "ui.pic,ui.look,u.dept_id as deptId,d.`name` as deptName," +
                    "(select count(*) as total from user_focus where user_focus_id = ui.user_id) as follows " +
                    "from user_info ui " +
                    "left join `user` u on u.id = ui.user_id " +
                    "left join dept d on d.id = u.dept_id " +
                    "where ui.user_id = ?";

            map = qr.query(sql, new MapHandler(), userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }
}
